<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="onConfirm"
    @cancel="onCancel"
  />
</template>

<script>
// v-model 事件名固定为input 属性名为value
// 在一个组件只能用一次
// 可以通过 子组件 model: { prop: '属性名' ，event:'方法名'} 修改属性名和事件名
import { editUserProfileAPI } from '@/api'
import dayjs from '@/utils/dayjs'
export default {
  name: '',
  components: {},
  props: {
    birthday: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.birthday)
    }
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: '更新生日中...',
        forbidClick: true
      })
      const birthday = dayjs(this.currentDate).format('YYYY-MM-DD')
      try {
        await editUserProfileAPI({
          birthday
        })
        this.$emit('update:birthday', birthday)
        this.$emit('close')
        this.$toast.success('更新生日成功')
      } catch (error) {
        this.$toast.fail('更新生日失败！请重新修改')
      }
    },
    onCancel() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped></style>
